package main

import (
	"fmt"
	"net"
)

func handle(conn net.Conn) {
	defer conn.Close()

	for {
		//创建一个切片,将读取的数据放入切片
		buf := make([]byte, 1024)

		n, err := conn.Read(buf)
		if err != nil {
			return
		}
		fmt.Println(string(buf[0:n]))
	}
}

func main() {
	fmt.Println("服务器启动")
	listen, err := net.Listen("tcp", "127.0.0.1:8080")
	if err != nil {
		fmt.Println("监听失败,err:", err)
	}

	for {
		conn, err2 := listen.Accept()
		if err2 != nil {
			fmt.Println("客户端接收失败,", err2)
		} else {
			fmt.Println("accept success,con=", conn, "clientip:", conn.RemoteAddr().String())
		}

		go handle(conn)
	}

}
